/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador.coordinadores;

import DBMS.DataBaseManager;
import clases.Coordinador;
import clases.Institucion;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author tamerdark
 */
public class Modificar extends DispatchAction {

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("instituciones", Institucion.getAll());
        Coordinador c = (Coordinador) form;
        System.out.println("La cedula del coordinador en modificar es: " + c.getCedula());
        DataBaseManager dbms = DataBaseManager.getInstance();
        boolean modificado = dbms.modificarCoordinador(c);
        if (modificado) {
            c.setMensaje("Coordinador modificado exitosamente");
            return mapping.findForward("success");
        } else {
            c.setMensaje("Problemas al modificar coordinador");
            return mapping.findForward("failure");
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("instituciones", Institucion.getAll());
        Coordinador c = (Coordinador) form;
        System.out.println("La cedula del coordinador en modificar es: " + c.getCedula());
        DataBaseManager dbms = DataBaseManager.getInstance();
        List<Coordinador> coordinadores = dbms.consultarCoordinador(c);
        if (coordinadores.size() > 0) {
            c.cloneWith(coordinadores.get(0));
            return mapping.findForward("success");
        } else {
            c.setMensaje("Problemas al modificar coordinador");
            return mapping.findForward("failure");
        }
    }
}
